package com.eshop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderOperateDao {

	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;

	public int orderDelete(String ordersId)
	{
		int i = 0;		
		try {
			conn = Conn.getConn();
			String sql = "UPDATE orders SET exist_admins = 0 WHERE orders_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, ordersId);
			i = sta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}
	
	public int ordersDeliver(String ordersId)
	{
		int i = 0;
		try {
			conn =  Conn.getConn();
			String sql = "UPDATE orders SET orders_status = 2 WHERE orders_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, ordersId);
			i = sta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
		return i;
	}
	
	public int ordersCancel(String ordersId)
	{
		int i = 0;
		try {
			conn =  Conn.getConn();
			String sql = "UPDATE orders SET orders_status = 0 WHERE orders_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, ordersId);
			i = sta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
		return i;		
	}
	
	public int ordersAgree(String ordersId)
	{
		int i = 0;
		try {
			conn =  Conn.getConn();
			String sql = "UPDATE orders SET orders_status = 3 WHERE orders_id = ?";
			sta = conn.prepareStatement(sql);
			sta.setObject(1, ordersId);
			i = sta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
		return i;		
	}
}
